import http from "wechat-http";

http.baseURL = "http://toutiao.itheima.net";

http.intercept.response = (res) => {
  return res;
};

http.intercept.request = (options) => {
  const token = getApp().token;

  // 指定默认的头信息
  const defaultHeader = {};
  // 权限认证
  defaultHeader.Authorization = "Bearer " + token;
  // 合并头信息
  options.header = Object.assign({}, defaultHeader, options.header);
  // 拦截器处理后的请求参数
  return options;
};

http.intercept.response = (res) => {
  // console.log('响应信息',res);
  if (res.statusCode === 401) {
    wx.removeStorageSync("HMTT-token");
    getApp().token = "";
    wx.utils.toast("登录已过期，请先登录哦");
  }
  return res;
};

wx.http = http;

export default http;
